package com.baseweb.webadmin.core.model.dao.impl;

import com.baseweb.webadmin.core.model.dao.MsgDao;
import com.baseweb.webadmin.core.model.mapper.PushRecordDetailMapper;
import com.baseweb.webadmin.core.model.mapper.PushRecordMapper;
import com.baseweb.webadmin.core.model.pojo.PushRecord;
import com.baseweb.webadmin.core.model.pojo.PushRecordDetail;
import com.baseweb.webadmin.core.model.pojo.PushRecordDetailExample;
import com.baseweb.webadmin.core.model.pojo.PushRecordExample;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by Administrator on 2014/9/4.
 */
public class MsgDaoImpl extends SqlSessionDaoSupport implements MsgDao{

    @Autowired
    private PushRecordMapper pushRecordMapper;

    @Autowired
    private PushRecordDetailMapper pushRecordDetailMapper;

    @Override
    public void insertPushRecord(PushRecord pushRecord) {
        pushRecordMapper.insert(pushRecord);
    }

    @Override
    public void updatePushRecord(PushRecord pushRecord) {
        pushRecordMapper.updateByPrimaryKeySelective(pushRecord);
    }

    @Override
    public PushRecord findPushRecordById(Integer id) {
        return pushRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public void insertPushRecordDetail(PushRecordDetail pushRecordDetail) {
        pushRecordDetailMapper.insert(pushRecordDetail);
    }

    @Override
    public void updatePushRecordDetail(PushRecordDetail pushRecordDetail) {
        pushRecordDetailMapper.updateByPrimaryKeySelective(pushRecordDetail);
    }

    @Override
    public PushRecordDetail findPushRecordDetailById(Integer id) {
        return pushRecordDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<PushRecord> findPushRecords(Integer start, Integer offset, Integer status) {
        PushRecordExample example = new PushRecordExample();
        PushRecordExample.Criteria criteria = example.createCriteria();
//        if (null != beginDate && null != endDate) {
//            criteria.andCreateDateGreaterThanOrEqualTo(beginDate);
//            criteria.andCreateDateLessThanOrEqualTo(endDate.plusDays(1));
//        }
        criteria.andStatusEqualTo(status);
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return pushRecordMapper.selectByExample(example);
    }

    @Override
    public Integer countPushRecords(Integer status) {
        PushRecordExample example = new PushRecordExample();
        PushRecordExample.Criteria criteria = example.createCriteria();
//        if (null != beginDate && null != endDate) {
//            criteria.andCreateDateGreaterThanOrEqualTo(beginDate);
//            criteria.andCreateDateLessThanOrEqualTo(endDate.plusDays(1));
//        }
        criteria.andStatusEqualTo(status);
        return pushRecordMapper.countByExample(example);
    }

    @Override
    public List<PushRecordDetail> findPushRecordDetails(Integer pushRecordId, Integer start, Integer offset, Integer status) {
        PushRecordDetailExample example = new PushRecordDetailExample();
        PushRecordDetailExample.Criteria criteria = example.createCriteria();
//        if (null != beginDate && null != endDate) {
//            criteria.andCreateDateGreaterThanOrEqualTo(beginDate);
//            criteria.andCreateDateLessThanOrEqualTo(endDate.plusDays(1));
//        }
        criteria.andRecordIdEqualTo(pushRecordId);
        criteria.andStatusEqualTo(status);
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return pushRecordDetailMapper.selectByExample(example);
    }

    @Override
    public Integer countPushRecordDetails(Integer pushRecordId, Integer status) {
        PushRecordDetailExample example = new PushRecordDetailExample();
        PushRecordDetailExample.Criteria criteria = example.createCriteria();
//        if (null != beginDate && null != endDate) {
//            criteria.andCreateDateGreaterThanOrEqualTo(beginDate);
//            criteria.andCreateDateLessThanOrEqualTo(endDate.plusDays(1));
//        }
        criteria.andRecordIdEqualTo(pushRecordId);
        criteria.andStatusEqualTo(status);
        return pushRecordDetailMapper.countByExample(example);
    }
}
